package com.gitee.wsl.transform.validator.string.ext

import com.gitee.wsl.transform.validator.ValidationError
import com.gitee.wsl.transform.validator.string.StringValidator

class RegexRuleValidator(val regex: Regex) : StringValidator {

    constructor(regexString: String):this(regexString.toRegex())

    override fun message(para: String): ValidationError {
        return ValidationError.DoesNotMatch("$para not regex",regex)
    }

    override fun validate(para: String): Boolean = regex.matches(para)
}

fun Regex.toValidator() = RegexRuleValidator(this)

fun StringValidator.Companion.regex(regexString: String) = RegexRuleValidator(regexString)

fun StringValidator.regex(regexString: String) = then(RegexRuleValidator(regexString))

fun StringValidator.regex(regex: Regex) = then(RegexRuleValidator(regex))